/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ImageSelection.java
 *
 * Created on 14/02/2009, 18:43:00
 */
package edu.upf.grupE.interftest;

import edu.upf.grupE.Controller.PictureController;
import edu.upf.grupE.Controller.VideoController;
import edu.upf.grupE.Entities.Album;
import edu.upf.grupE.Entities.FileDescriptor;
import edu.upf.grupE.Entities.TextDescription;
import edu.upf.grupE.Entities.Video;
import edu.upf.grupE.Entities.Videos;
import edu.upf.grupE.impl.GUILogic;
import java.net.URL;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.JPanel;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

/**
 *
 * @author Oscar
 */
public class VideoSelection extends javax.swing.JPanel {

    DefaultMutableTreeNode first;
    ArrayList[] vid;
    DefaultTreeModel modelo;
    Video demo;
    ImageSelection im;
    RepMulti m;

    /** Creates new form ImageSelection */
    public VideoSelection() {
       
        
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton4 = new javax.swing.JButton();
        jFrame1 = new javax.swing.JFrame();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();
        jXPanel1 = new org.jdesktop.swingx.JXPanel();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();

        jButton4.setText("jButton4");

        javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        setPreferredSize(new java.awt.Dimension(952, 450));

        jLabel1.setText("Albums");

        jScrollPane1.setPreferredSize(new java.awt.Dimension(74, 472));

        first = new DefaultMutableTreeNode("My Albums");
        DefaultTreeModel modelo= new DefaultTreeModel(first);
        this.sintext();
        this.changeState(false);
        jTree1.setModel(modelo);
        this.UpdateAlbums();

        /*
        DefaultMutableTreeNode padre = new DefaultMutableTreeNode("Parties");
        DefaultMutableTreeNode tio = new DefaultMutableTreeNode("Barna02-03.mpg");

        DefaultMutableTreeNode hijo=new DefaultMutableTreeNode("Holidays");
        DefaultMutableTreeNode hija=new DefaultMutableTreeNode("Rome.avi");
        modelo.insertNodeInto(padre,abuelo,0);
        modelo.insertNodeInto(tio,padre,0);
        modelo.insertNodeInto(hijo, abuelo, 1);

        modelo.insertNodeInto(hija, hijo, 0);*/
        jTree1.addTreeSelectionListener(new TreeSelectionListener() {
            public void valueChanged(TreeSelectionEvent e) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode)
                jTree1.getLastSelectedPathComponent();

                if(node.getChildCount()==0){
                    int index= node.getParent().getIndex(node);
                    int indmain=first.getIndex(node.getParent());
                    Video auxiliar= (Video)vid[indmain].get(index);
                    demo=auxiliar;
                    String albactual= node.getParent().toString();

                    try{
                        String a=auxiliar.getFileDescriptor().getPath();
                        m= new RepMulti();
                        m.Stop();
                        m.Play("File:"+auxiliar.getFileDescriptor().getPath());
                        sintext();
                        changeState(true);
                        showDetails(demo);

                        //jXPanel2.add(new MediaPlayer(new URL("file:"+auxiliar.getFileDescriptor().getPath())));
                    }catch(Exception exc){}

                    //    jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/ima_new_paisaje_001.jpg"))); // NOI18N
                }
            }
        });
        jTree1.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                jTree1ValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jTree1);

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/Leopard Icon (126).png"))); // NOI18N
        jButton2.setText("Upload Youtube");
        jButton2.setBorderPainted(false);
        jButton2.setContentAreaFilled(false);
        jButton2.setEnabled(false);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/kino.png"))); // NOI18N
        jButton3.setText("Add Video to Album");
        jButton3.setBorderPainted(false);
        jButton3.setContentAreaFilled(false);
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/Leopard Icon (70).png"))); // NOI18N
        jButton7.setText("Delete Album");
        jButton7.setBorderPainted(false);
        jButton7.setContentAreaFilled(false);
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jXPanel1Layout = new javax.swing.GroupLayout(jXPanel1);
        jXPanel1.setLayout(jXPanel1Layout);
        jXPanel1Layout.setHorizontalGroup(
            jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel1Layout.createSequentialGroup()
                .addGap(51, 51, 51)
                .addComponent(jButton2)
                .addGap(48, 48, 48)
                .addComponent(jButton3)
                .addGap(18, 18, 18)
                .addComponent(jButton7)
                .addContainerGap(337, Short.MAX_VALUE))
        );
        jXPanel1Layout.setVerticalGroup(
            jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton3)
                    .addComponent(jButton7))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        jLabel4.setText("Author:");

        jLabel5.setText("Name:");

        jLabel6.setText("Description:");

        jLabel7.setText("Keywords:");

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel8.setText("Video Details:");

        jButton1.setText("Save");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton8.setText("Delete");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 8));
        jLabel9.setForeground(new java.awt.Color(255, 0, 51));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(80, 80, 80)
                                .addComponent(jLabel2))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(189, 189, 189)
                                .addComponent(jLabel3))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(jXPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(528, 528, 528)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jLabel4)
                                            .addComponent(jLabel5)
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jLabel6)
                                                .addComponent(jLabel7))
                                            .addComponent(jLabel8)
                                            .addComponent(jLabel9))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(jTextField4)
                                            .addComponent(jTextField3)
                                            .addComponent(jTextField2)
                                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGap(22, 22, 22))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addComponent(jButton1)
                                        .addGap(18, 18, 18)
                                        .addComponent(jButton8)
                                        .addGap(24, 24, 24)))
                                .addGap(223, 223, 223)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(57, 57, 57)
                        .addComponent(jLabel2)
                        .addGap(19, 19, 19)
                        .addComponent(jLabel3)
                        .addGap(117, 117, 117)
                        .addComponent(jLabel8)
                        .addGap(5, 5, 5)
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton8)
                            .addComponent(jButton1))
                        .addGap(71, 71, 71)
                        .addComponent(jXPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    public void setSIM(ImageSelection im){
        this.im=im;
    }
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        SelectVideo change = new SelectVideo(im, this);
        change.setVisible(true);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
    
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jTree1ValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_jTree1ValueChanged
    }//GEN-LAST:event_jTree1ValueChanged

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        // TODO add your handling code here:
        DelAlbum delal= new DelAlbum(im, this);
        delal.setVisible(true);
    }//GEN-LAST:event_jButton7ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
      if (jTextField2.getText().length() == 0) {
            jLabel9.setText("Error: Name Field Required");
        } else {

            if ((jTextField4.getText().length() > 0) & (jTextField4.getText().indexOf(",") == -1)) {
                jLabel9.setText("Error: A Minimum of a comma is required in the description field");
            } else {
                jLabel9.setText("");

                demo.setName(jTextField2.getText());



                demo.addAuthor(0,jTextField1.getText());

                TextDescription t = new TextDescription();
                t.setDescription(jTextField3.getText());

                String keys[] = jTextField4.getText().split(",");
                int i=0;
                for (String key : keys) {

                   t.addUserKeywords(i, key);
                   i++;
                }



                demo.setTextDescription(t);
                demo = VideoController.getById(VideoController.createOrUpdate(demo));
            }

        }

       
}//GEN-LAST:event_jButton1ActionPerformed


    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
        // TODO add your handling code here:
         this.changeState(false);
        this.sintext();
        GUILogic.DeletePicture(demo.getId());
        m.hide();
        m.Stop();
        this.UpdateAlbums();
        im.UpdateAlbums();
}//GEN-LAST:event_jButton8ActionPerformed
  public void UpdateAlbums() {
        first = new DefaultMutableTreeNode("My Albums");
        modelo= new DefaultTreeModel(first);
        Vector<Album> albums = GUILogic.getListAlbums();
        vid = new ArrayList[albums.size()];
        System.out.println(albums.size());
        for (int i = 0; i < albums.size(); i++) {
            DefaultMutableTreeNode t = new DefaultMutableTreeNode(albums.get(i).getName());
            modelo.insertNodeInto(t, first, i);
            vid[i]=new ArrayList();
            Vector<Video> videos = GUILogic.getVideosByAlbum(albums.get(i).getId());
            for (int j = 0; j < videos.size(); j++) {
                vid[i].add(videos.get(j));
                modelo.insertNodeInto(new DefaultMutableTreeNode(videos.get(j).getName()), t, j);
            }
        }
      jTree1.setModel(modelo);
    }


 public void changeState(boolean x){
    jButton1.setEnabled(x);
    jButton8.setEnabled(x);
    jTextField1.setEnabled(x);
    jTextField2.setEnabled(x);
    jTextField3.setEnabled(x);
    jTextField4.setEnabled(x);



}

public void showDetails(Video vid){
   this.changeState(true);
   String[] author=vid.getAuthor();
   String name= vid.getName();
   String description= vid.getTextDescription().getDescription();
   String[] keywords= vid.getTextDescription().getUserKeywords();
   if(author[0]==null){
       jTextField1.setText("");
   }else{
       jTextField1.setText(author[0]);
   }
      if(name==null){
       jTextField2.setText("");
   }else{
       jTextField2.setText(name);
   }
      if(description==null){
       jTextField3.setText("");
   }else{
       jTextField3.setText(description);
   }
      if(keywords==null){
       jTextField4.setText("");
   }else{
       String key="";

       for(int i=0; i< keywords.length; i++){
            if(!keywords[i].equals("")){
       key=key+keywords[i]+", ";}
       }
       jTextField4.setText(key);
   }
}

public void sintext(){
    jTextField1.setText("");
    jTextField2.setText("");
    jTextField3.setText("");
    jTextField4.setText("");


}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTree jTree1;
    private org.jdesktop.swingx.JXPanel jXPanel1;
    // End of variables declaration//GEN-END:variables
}
